<?php

use yii\helpers\Inflector;
use yii\helpers\StringHelper;

/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

/* @var $model \yii\db\ActiveRecord */
$model = new $generator->modelClass();
$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
    $safeAttributes = $model->attributes();
}

echo "<?php\n";
?>

use yii\helpers\Html;
use backend\themes\pearlayui\widgets\ActiveForm;
use backend\themes\pearlayui\widgets\Tabs;

/* @var $this yii\web\View */
/* @var $model <?= ltrim($generator->modelClass, '\\') ?> */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-form" style="padding: 15px;">
    <div class="layui-card-header"><h2><?= "<?= " ?> Html::encode($this->title) ?></h2></div>
    
<div class="layui-card-body">
    <?= "<?php " ?>$form = ActiveForm::begin(); ?>
	<?= "<?php " ?> echo Html::hiddenInput('urlReferrer',!empty($list['urlReferrer']) ? $list['urlReferrer'] : null);?>
    
    <div class="x_title">
        
        <div class="layui-form-item">
        <label class="layui-form-label"></label>
            <?= "<?=" ?> Html::button('save', ['class' => 'layui-btn layui-btn-normal', 'lay-submit'=>'', 'lay-filter'=>'saveBtn']) ?>
        </div>
        <div class="clearfix"></div>
    </div>
    <div class="x_content">
<?php foreach ($generator->getColumnNames() as $attribute) {
    if (in_array($attribute, $safeAttributes)) {
        echo "    <?= " . $generator->generateActiveField($attribute) . " ?>\n\n";
    }
} ?>
    </div>
    <?= "<?php " ?>ActiveForm::end(); ?>
</div>

</div>
<script>
    var pid = 0;
    layui.use(['form', 'layer'], async function () {
        var form = layui.form,
            layer = layui.layer;

        /**
         * 初始化表單，要加上，不然刷新部分組件可能會不加載
         */
        form.render();
        
        // 當前彈出層，防止ID被覆蓋
        var parentIndex = layer.index;

        //監聽提交
        form.on('submit(saveBtn)', function (data) {
            
            $.ajax({
                url: $(this).parents("form").attr("action"),
                data: data.field,
                type: "POST",
                success: function(data){
                    if(data.msg){
                        var index = layer.alert(data.msg, {
                            title: '<?= "<?=" ?>Yii::t('app', 'Info')?>'
                        }, function () {
                            // 關閉彈出層
                            layer.close(index);
                            if(data.status){
                                layer.close(parentIndex);                                
                            }
                        });
                    }                    
                },
                error:function(err){
                    console.log(err);
                }
            });
            
            return false;
        });

    });
</script>